{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "quantitative-rochester",
   "metadata": {},
   "source": [
    "## 四、数学优化编程（本大题15分）"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "amino-designer",
   "metadata": {},
   "source": [
    "6、使用批量梯度下降算法拟合多维数据。待拟合的数据点为样本点对应的x值：[[6, 2], [8, 1], [10, 0], [14, 2], [18, 0]])，样本点对应的y值：[19, 21, 23, 43, 47])。上述数据点是根据函数$y=3 x_1+4 x_2-7$生成的。（15分）"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "national-complexity",
   "metadata": {},
   "source": [
    "### 程序源代码："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "mexican-float",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第 0 轮迭代: 损失 = 230.852704\n",
      "第 100 轮迭代: 损失 = 4.226633168271201\n",
      "第 200 轮迭代: 损失 = 3.3205362544383292\n",
      "第 300 轮迭代: 损失 = 2.856906576034195\n",
      "第 400 轮迭代: 损失 = 2.4820598164490306\n",
      "第 500 轮迭代: 损失 = 2.158523161335154\n",
      "第 600 轮迭代: 损失 = 1.8773459194921518\n",
      "第 700 轮迭代: 损失 = 1.6328121826328086\n",
      "第 800 轮迭代: 损失 = 1.4201316210748192\n",
      "第 900 轮迭代: 损失 = 1.235153710041398\n",
      "拟合参数: [2.76197875 3.11373845]\n",
      "截距项: -3.1815562730661338\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "# 数据点\n",
    "X = np.array([[6, 2], [8, 1], [10, 0], [14, 2], [18, 0]])\n",
    "y = np.array([19, 21, 23, 43, 47])\n",
    "\n",
    "# 初始化参数\n",
    "learning_rate = 0.01\n",
    "epochs = 1000\n",
    "n = len(y)\n",
    "weights = np.zeros(X.shape[1] + 1)  # 加上截距项\n",
    "\n",
    "# 增加一列1作为截距项\n",
    "X = np.insert(X, 0, 1, axis=1)\n",
    "\n",
    "# 定义损失函数\n",
    "def mse_loss(X, y, weights):\n",
    "    predictions = np.dot(X, weights)\n",
    "    return np.mean((predictions - y) ** 2)\n",
    "\n",
    "# 批量梯度下降\n",
    "for epoch in range(epochs):\n",
    "    predictions = np.dot(X, weights)\n",
    "    error = predictions - y\n",
    "    gradient = np.dot(X.T, error) / n\n",
    "    weights -= learning_rate * gradient\n",
    "\n",
    "    if epoch % 100 == 0:\n",
    "        loss = mse_loss(X, y, weights)\n",
    "        print(f\"第 {epoch} 轮迭代: 损失 = {loss}\")\n",
    "\n",
    "# 输出最终拟合的参数\n",
    "print(\"拟合参数:\", weights[1:])  # 权重项\n",
    "print(\"截距项:\", weights[0])  # 截距项\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "infectious-equivalent",
   "metadata": {},
   "source": [
    "### 结果分析："
   ]
  },
  {
   "cell_type": "markdown",
   "id": "exterior-hanging",
   "metadata": {},
   "source": [
    "这个代码使用了批量梯度下降算法来拟合数据，并得到了一组参数。现在让我们来分析一下这些结果：\n",
    "\n",
    "1. **迭代过程中的损失值变化**：\n",
    "   - 每隔100次迭代，代码会打印出损失值，损失值的下降是期望的，表示模型逐步在学习数据。\n",
    "\n",
    "2. **拟合参数**：\n",
    "   - 最终输出了拟合得到的参数，包括权重项和截距项。\n",
    "   - 权重项对应于模型中每个特征的系数，即参数3和4。\n",
    "   - 截距项表示模型的截距，即参数-7。\n",
    "\n",
    "3. **数据拟合效果**：\n",
    "   - 如果模型的拟合效果良好，那么得到的参数应该接近真实函数的系数（3、4和-7）。\n",
    "   - 如果模型的拟合效果不佳，那么得到的参数可能会与真实值有较大差距。\n",
    "\n",
    "4. **参数解释**：\n",
    "   - 参数3对应于特征𝑥₁的系数，参数4对应于特征𝑥₂的系数，参数-7对应于截距项。\n",
    "   - 在这个例子中，参数3和4应接近3和4，参数-7应接近-7，以使模型能够较好地拟合数据。\n",
    "\n",
    "5. **模型改进**：\n",
    "   - 如果拟合效果不佳，可以尝试调整学习率、迭代次数等超参数，或者使用其他更复杂的模型进行拟合。\n",
    "\n",
    "这种分析可以帮助我们理解拟合过程中的损失变化，以及最终得到的拟合参数与真实模型参数之间的关系。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b102813d-a198-4f45-83db-3af253e2dd44",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
